import anim_util from "./anim_util";
import wx_opendata from "./wx/wx_opendata";
import GameController from "./game_controller";
import global from "./global";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

declare const wx;

@ccclass
export default class ScoreContainer extends cc.Component {

    
    @property({type:cc.Label})
    startScoreLabel:cc.Label=null;

    @property({type:cc.Label})
    endScoreLabel:cc.Label=null;

    @property({type:cc.Node})
    scoreRoad:cc.Node=null;

    @property({type:cc.Node})
    playerView:cc.Node=null;

    @property({type:cc.Sprite})
    playerAvatar:cc.Sprite=null;

    @property({type:cc.Label})
    playerScore:cc.Label=null;

    @property(cc.Node)
    historyScoreNode:cc.Node=null;

    scoreSection:number=5000;

    preScore=0;

    subContextView:cc.WXSubContextView;

    // @property({type:cc.Sprite})

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        (this.playerScore as any)._isBold=true;
        (this.startScoreLabel as any)._isBold=true;
        (this.endScoreLabel as any)._isBold=true;
         this.subContextView=this.historyScoreNode.getComponent(cc.WXSubContextView);
    }

    start () {
        if (CC_WECHATGAME) {
            wx_opendata.wxPostMessageToSubDomain({action:wx_opendata.WxDomainAction.FetchHistoryScore});
            this.subContextView.updateSubContextViewport();
            this.scheduleOnce(()=>{
                  this.subContextView.enabled=false;
            },5);
        }
    }
    init(){
        this.playerView.setPosition(-1*this.scoreRoad.width/2,this.playerView.y);
        this.playerScore.string="0";
        this.startScoreLabel.string="0";
        this.endScoreLabel.string=""+this.scoreSection;

        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        this.playerAvatar.spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        this.playerAvatar.node.active = false;
                    }
                };
                image.src = global.userInfo.avatarUrl;
            } catch (e) {
                cc.log(e);
            }
        } 
    }

    changeScore(value){
        if(value===0){
            this.init();
            return;
        }
        this.playerScore.string=""+value;
        this.playerScore.node.runAction(cc.sequence(cc.scaleTo(0.2,1.3,1.3),cc.scaleTo(0.2,1,1)));
        let score2=value-this.preScore;
        this.preScore=value;
        let distance=score2;
        let actions=[];
        let curPosX=this.playerView.x;
        cc.log(distance);
        while(distance!==0){
            if(distance>this.scoreSection){
                distance-=this.scoreSection;
                actions.push(cc.moveTo(0.5,this.endScoreLabel.node.x,this.playerView.y));
                actions.push(cc.callFunc(()=>{
                    this.playerView.setPosition(-1*this.scoreRoad.width/2,this.playerView.y);
                }));
            }
            else{
                let distance2=this.scoreRoad.width/this.scoreSection*distance;
                actions.push(cc.moveTo(0.5,curPosX+distance2,this.playerView.y));
                actions.push(cc.callFunc(()=>{
                   if(value>Number(this.endScoreLabel.string)){
                       this.startScoreLabel.string=this.endScoreLabel.string;
                       this.endScoreLabel.string=""+Number(this.endScoreLabel.string)*2;
                       this.playerView.setPosition(-1*this.scoreRoad.width/2,this.playerView.y);
                   }
                }));
                distance=0;
            }
        }
        this.playerView.runAction(cc.sequence(actions));

    }

    // update (dt) {}
}
